var baseurl,oneclickprice;
	var regioncount = 1;
	function changeType(value) {
		if (value == "1") {
			$("#videourl").show();
			$("#imgresources").hide();
		} else {
			$("#videourl").hide();
			$("#imgresources").show();
		}
	}

	function u_bonus_save_submit() {
		$("#form-u_bonus-add").Validform({
			tiptype : 2,
			ajaxPost : true,
			callback : function(form) {
				var index = parent.layer.getFrameIndex(window.name);
					if (index) {//关闭弹出层
						parent.$('.btn-refresh').click();
						parent.layer.close(index);
					} else {//单独的页面
						location.href = baseurl + "/backstage/u_bonus-list";
					}
				}
			});
	}

		// 图片上传demo
		$(function() {
			oneclickprice = $("#oneclickprice").val();
			regioncount = $("#regiondiv").children().length;
			//失去焦点事件，更新点击次数和曝光次数
			$("#summy").blur(function(){
				var clickcount = parseFloat($(this).val())/parseFloat(oneclickprice);
				$("#clickcount").val(clickcount);
				$("#show_times").val(clickcount*3);
			});

			baseurl = '<%=path%>';
			/*企业Logo上传*/
			/*  var $list = $('#fileList_logo'), */
			var $list_covers = $('#fileList_covers'), $list_resources = $('#fileList_resources'), $list_video = $('#fileList_video'), ratio = window.devicePixelRatio || 1, thumbnailWidth = 100 * ratio, thumbnailHeight = 100 * ratio, uploader_resources, uploader_covers, uploader_video;

			/*广告封面上传*/
			uploader_covers = WebUploader.create({
				auto : true,
				swf : 'lib/webuploader/0.1.5/Uploader.swf',
				server : baseurl + '/backstage/common/uploadPic',
				pick : '#filePicker_covers',
				accept : {
					title : 'Images',
					extensions : 'gif,jpg,jpeg,bmp,png',
					mimeTypes : 'image/jpg,image/jpeg,image/png'
				},
				formData : {
					foldername : 'advert'
				}
			});

			uploader_covers
					.on(
							'fileQueued',
							function(file) {
								var $li = $('<div id="' + file.id + '" class="file-item thumbnail">'
										+ '<img>'
										+ '<div class="info">'
										+ file.name + '</div>' + '</div>'), $img = $li
										.find('img');

								$list_covers.html($li);
								uploader_covers.makeThumb(file, function(error,
										src) {
									if (error) {
										$img.replaceWith('<span>不能预览</span>');
										return;
									}

									$img.attr('src', src);
								}, thumbnailWidth, thumbnailHeight);
							});

			uploader_covers.on('uploadSuccess', function(file, response) {
				$('#' + file.id).addClass('upload-state-done');
				$("#covers").val(response.datas);
			});

			uploader_covers.on('uploadError', function(file) {
				var $li = $('#' + file.id), $error = $li.find('div.error');
				if (!$error.length) {
					$error = $('<div class="error"></div>').appendTo($li);
				}

				$error.text('上传失败');
			});

			uploader_covers.on('uploadComplete', function(file) {
				$('#' + file.id).find('.progress').remove();
			});

			/*轮播图上传*/
			uploader_resources = WebUploader.create({
				auto : true,
				swf : 'lib/webuploader/0.1.5/Uploader.swf',
				server : baseurl + '/backstage/common/uploadPic',
				pick : '#filePicker_resources',
				accept : {
					title : 'Images',
					extensions : 'gif,jpg,jpeg,bmp,png',
					mimeTypes : 'image/jpg,image/jpeg,image/png'
				},
				formData : {
					foldername : 'advert'
				}
			});

			uploader_resources
					.on(
							'fileQueued',
							function(file) {
								var $li = $('<div id="' + file.id + '" class="file-item thumbnail">'
										+ '<img>'
										+ '<div class="info"><input type="hidden" value="" name="resources" id="resource' + file.id + '"/></div>'
										+ '</div>'), $img = $li.find('img');

								$list_resources.append($li);
								uploader_resources.makeThumb(file, function(
										error, src) {
									if (error) {
										$img.replaceWith('<span>不能预览</span>');
										return;
									}

									$img.attr('src', src);
								}, thumbnailWidth, thumbnailHeight);
							});

			uploader_resources.on('uploadSuccess', function(file, response) {
				$('#' + file.id).addClass('upload-state-done');
				$("#resource" + file.id).val(response.datas);
			});

			uploader_resources.on('uploadError', function(file) {
				var $li = $('#' + file.id), $error = $li.find('div.error');
				if (!$error.length) {
					$error = $('<div class="error"></div>').appendTo($li);
				}
				$error.text('上传失败');
			});

			uploader_resources.on('uploadComplete', function(file) {
				$('#' + file.id).find('.progress').remove();
			});

			/*视频上传上传*/
			uploader_video = WebUploader.create({
				auto : true,
				swf : 'lib/webuploader/0.1.5/Uploader.swf',
				server : baseurl + '/backstage/common/uploadPic',
				pick : '#filePicker_video',
				accept : {
					title : 'Images',
					extensions : 'gif,jpg,jpeg,bmp,png,mp4',
					mimeTypes : 'video/mp4'
				},
				formData : {
					foldername : 'advert'
				}
			});

			uploader_video
					.on(
							'fileQueued',
							function(file) {
								var $li = $('<div id="' + file.id + '" class="file-item thumbnail">'
										+ '<img>'
										+ '<div class="info">'
										+ file.name + '</div></div>'), $img = $li
										.find('img');

								$list_video.append($li);
								uploader_video.makeThumb(file, function(error,
										src) {
									if (error) {
										$img.replaceWith('<span>不能预览</span>');
										return;
									}

									$img.attr('src', src);
								}, thumbnailWidth, thumbnailHeight);
							});

			uploader_video.on('uploadSuccess', function(file, response) {
				$('#' + file.id).addClass('upload-state-done');
				$("#video").val(response.datas);
			});

			uploader_video.on('uploadError', function(file) {
				var $li = $('#' + file.id), $error = $li.find('div.error');
				if (!$error.length) {
					$error = $('<div class="error"></div>').appendTo($li);
				}
				$error.text('上传失败');
			});

			uploader_video.on('uploadComplete', function(file) {
				$('#' + file.id).find('.progress').remove();
			});
		});

		function addregion() {
			regioncount++;
			var html = '<div class="row cl"><label class="form-label col-xs-4 col-sm-3">投放地区：</label>'
					+ '<div class="formControls col-xs-8 col-sm-5">'
					+ '<input id="city-picker'+regioncount+'" class="form-control" readonly name="regioninfo" type="text" value="" data-toggle="city-picker"></div>'
					+ '<div class="formControls col-1"><button onclick="delregion(this)" type="button">删除</button></div></div>';
			$("#regiondiv").append(html);
			var $newcitypicker = $('#city-picker' + regioncount);
			$newcitypicker.citypicker();
			$('#reset').click(function() {
				$newcitypicker.citypicker('reset');
			});

			$('#destroy').click(function() {
				$newcitypicker.citypicker('destroy');
			});

			if (regioncount > 1) {
				$("#range").hide();
			}
		}

		function delregion(obj) {
			$(obj).parent().parent().remove();
			regioncount = $("#regiondiv").children().length;
			if (regioncount == 1) {
				$("#range").show();
			}
		}

		function getlnglat() {
			var index = layer.open({
				type : 2,
				title : "地图定位",
				content : baseurl + "/backstage/common/baiduditu?lng="
						+ $("#longitude").val() + "&lat="
						+ $("#latitude").val()
			});
			layer.full(index);
		}

		function loadlnglat(lng, lat) {
			$("#longitude").val(lng);
			$("#latitude").val(lat);
		}